﻿using MiniFox;
using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using Platform.Core.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Platform.Data
{
    [AutoResolve]
    public class GenerationProvider : EntityDataProvider<PlatformDbContext>, IGenerationProvider
    {
        public (SequenceSetting, int) NextVal(string name)
        {
            var entity = this.CurrentDbContext.SequenceSet.SingleOrDefault(s => s.Name == name);
            if (entity == null) return default;
            entity.CurrentVal++;
            if (entity.CurrentVal > entity.MaxVal)
            {
                entity.CurrentVal = 1;
            }

            if (this.Update(entity) <= 0) return default;
            var setting = new SequenceSetting(entity.Prefix, entity.Suffix, entity.Pattern, entity.Length, entity.BaseOnUtc, MiniFox.Utility.CodeType.Number);
            return (setting, entity.CurrentVal);
        }
    }
}
